package net.sxnic.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sxnic.comm.CommConstant;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

public class PropertiesUtils {

	public static Map<String, String> PROPERTIES_MAP = new HashMap<String, String>();

	public static String path = "classpath:globalMessages;classpath:application";

	public static String getText(String key) {
		if (PROPERTIES_MAP.containsKey(key)) {
			return PROPERTIES_MAP.get(key);
		} else {
			return "not key";
		}
	}

	public static String getText(String key, String[] repls) {
		if (PROPERTIES_MAP.containsKey(key)) {
			return PROPERTIES_MAP.get(key);
		} else {
			return "not key";
		}
	}

	@Test
	public static void read() {

		try {
			if(CommConstant.PROPERTY_MAP.containsKey("sys_properies_files")){
				path = CommConstant.PROPERTY_MAP.get("sys_properies_files");
			}
			// 暂时只支持classpath下的属性文件
			String[] p_files = StringUtils.split(path,";");
			Configuration config;
			Iterator<?> iter;
			for (String pf : p_files) {
				if (StringUtils.startsWith(pf, "classpath:")) {
					pf = StringUtils.remove(pf, "classpath:");
				}

				// TODO 国际化属性文件
				if (!pf.endsWith("_zh_CN.properties")) {
					pf = pf + "_zh_CN.properties";
				}

				if (!pf.endsWith(".properties")) {
					pf = pf + ".properties";
				}

				config = new PropertiesConfiguration(pf);
				iter = config.getKeys();
				while (iter.hasNext()) {
					String key = (String) iter.next();
					PROPERTIES_MAP.put(key, config.getString(key));
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//
	}

}
